#!/usr/bin/env python
# -*- coding:utf-8 -*-

from fabric.api import *
from fabric.contrib.files import exists

BASE_PATH = '/home/xu.du/apps/'
APP_PATH = BASE_PATH + '51job'
APP_NAME = 'job51_by_company_id.py'


@parallel(pool_size=30)
def deploy_app():
    if not exists(APP_PATH):
        run('mkdir -p ' + APP_PATH)
    with settings(warn_only=True):
        sudo('rm -Rf ' + APP_PATH + '/*')
    put('*', APP_PATH + '/')


@parallel(pool_size=30)
def run_app():
    with cd(APP_PATH):
        run('$(nohup python {app} >& /dev/null < /dev/null &) && sleep 1'.format(app=APP_NAME), shell='/bin/bash')


@parallel(pool_size=30)
def stop():
    with settings(warn_only=True):
        run('pkill -f ' + APP_NAME)


@parallel(pool_size=30)
def test():
    with settings(warn_only=True):
        sudo('pip install xtls --upgrade')


if __name__ == "__main__":
    all_vps = [
        "xu.du@120.26.94.163",
        "xu.du@114.215.242.51",
        "xu.du@120.26.94.76",
        "xu.du@120.55.89.151",
        "xu.du@121.40.98.252",
        "xu.du@121.43.61.53",
        "xu.du@120.55.185.160",
        "xu.du@114.215.242.42",
        "xu.du@120.26.222.41",
        "xu.du@114.215.209.110",
        "xu.du@114.215.242.111",
    ]

    env.hosts = all_vps
    env.password = 'qwerfdsa'
    execute(deploy_app)
    execute(run_app)
    # execute(stop)
    # execute(test)
